package com.wenhuitec.android.tang.config;


import android.content.Context;
import android.content.SharedPreferences;

import com.dhkj.web.bean.AddrTree;
import com.dhkj.web.bean.Bmzl;
import com.dhkj.web.bean.TangReportTime;
import com.dhkj.web.bean.Zweekmenubak;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SystemInit {
    private static SystemInit systemInit;

    private List<AddrTree> addrTreeList = new ArrayList<>();

    private List<TangReportTime> tangReportTimeList = new ArrayList<>();

    private Map<Integer, List<Bmzl>> bmzlList = new HashMap<>();

    private Map<Integer, List<Zweekmenubak>> zWeekMenuBakMap = new HashMap<>();

    private Context mContext;

    private int printDeviceType;

    private int dzkCodeType;


    public static SystemInit getInstance(Context context) {
        if (systemInit == null) {
            systemInit = new SystemInit(context);
        }
        return systemInit;

    }

    public static void clear() {
        systemInit = null;
    }


    public SystemInit(Context context) {
        //获取系统参数
        synchronized (this) {
            mContext = context;
            SharedPreferences sharedPreferences =
                    context.getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
            String user = sharedPreferences.getString("user", "");

            RequestUtils.getAddr(context, user, new MyObserverRes<List<AddrTree>>(context) {
                @Override
                public void onSuccess(List<AddrTree> result) {
                    addrTreeList = result;
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });


            //----------------------------------------------------------
            RequestUtils.getBmzl(context, new MyObserverRes<Map<Integer, List<Bmzl>>>(context) {
                @Override
                public void onSuccess(Map<Integer, List<Bmzl>> result) {
                    bmzlList = result;
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });
            //----------------------------------------------------------
            RequestUtils.getZWeekMenuBak(context, new MyObserverRes<Map<Integer, List<Zweekmenubak>>>(context) {
                @Override
                public void onSuccess(Map<Integer, List<Zweekmenubak>> result) {
                    zWeekMenuBakMap = result;
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });


            RequestUtils.getReportTime(context, user, new MyObserverRes<List<TangReportTime>>(context) {
                @Override
                public void onSuccess(List<TangReportTime> result) {
                    tangReportTimeList = result;
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {

                }
            });
            this.printDeviceType = sharedPreferences.getInt("printDevice", 58);
            this.dzkCodeType = sharedPreferences.getInt("dzkCodeType", 1);
        }
    }

    //获取地址库
    public List<AddrTree> getAddrTreeList() {
        return addrTreeList;
    }

    public Map<Integer, List<Bmzl>> getBmzlList() {
        return bmzlList;
    }

    public Map<Integer, List<Zweekmenubak>> getzWeekMenuBakMap() {
        return zWeekMenuBakMap;
    }

    public List<TangReportTime> getTangReportTimeList() {
        return tangReportTimeList;
    }

    public int getPrintDeviceType() {
        return printDeviceType;
    }

    public void setPrintDeviceType(int printDeviceType) {
        this.printDeviceType = printDeviceType;
        SharedPreferences sharedPreferences =
                mContext.getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putInt("printDevice", printDeviceType);
        editor.commit();
    }

    public int getDzkCodeType() {
        return dzkCodeType;
    }

    public void setDzkCodeType(int dzkCodeType) {
        this.dzkCodeType = dzkCodeType;
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
        SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
        editor.putInt("dzkCodeType", dzkCodeType);
        editor.commit();
    }
}
